package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HomeNewsContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HomeNewsBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ReqParamsGetHomeNews;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 *
 */


@ActivityScope
public class HomeNewsPresenter extends BasePresenter<HomeNewsContract.Model, HomeNewsContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;

    @Inject
    public HomeNewsPresenter(HomeNewsContract.Model model, HomeNewsContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }

    /**
     * 获取主页资讯
     *
     * @param reqParamsGetHomeNews
     */
    public void getHomeNews(ReqParamsGetHomeNews reqParamsGetHomeNews) {
        mModel.getHomeNews(AppUtil.entityToMap(reqParamsGetHomeNews)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HomeNewsBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HomeNewsBean>> resultBean) {
                        mRootView.hideLoading();
                        if (AppUtil.isSuccess(resultBean)) {
                            mRootView.getHomeNewsSuccess(resultBean.getData());
                        } else {
                            mRootView.showMessage(resultBean.getMessage());
                            mRootView.getHomeNewsFailed();
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}